#include "stdafx.h"

#undef UNICODE
/*
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glext.h>
*/
#include "texture_manager.h"
#include "image.h"

CTextureManager* CTextureManager::_inst = 0;

unsigned int CTextureManager::loadTexture(const char* file)
{
	GLint ret;
	CBitmapImage img;
	if (img.load(file) == FALSE)
		return -1;

	GLuint texture;
	// allocate a texture name
	glGenTextures(1, &texture);
	// select our current texture
	glBindTexture(GL_TEXTURE_2D, texture);
	// select modulate to mix texture with color for shading
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// when texture area is small, bilinear filter the closest mipmap
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	// when texture area is large, bilinear filter the original
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	// the texture wraps over at the edges (repeat)
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	// build our texture mipmaps
	ret = gluBuild2DMipmaps(GL_TEXTURE_2D, img.getBpp(), img.getWidth(), img.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, (const void *)img.getData());
	img.unload();
	if (ret == 0)
		return texture;

	glDeleteTextures(1, &texture);
	return 0;
}